package com.frxuexi.user.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.paging.MyDataSource
import com.frxuexi.user.repository.AskRepository
import com.frxuexi.user.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class AskViewModel @Inject constructor(private val askRepository: AskRepository):BaseViewModel<AskIntent> (){
    override fun handleIntent(intent: AskIntent) {
        when(intent){
            is AskIntent.askFor->{
                roomRequest(askRepository.askFor(intent.askLeave))
            }
            is AskIntent.askListByUid->{
                roomRequest(askRepository.askListByUid(intent.uid))
            }
            is AskIntent.selectByNoApprove->{
                roomRequest(askRepository.selectByNoApprove())
            }
            is AskIntent.approve->{
                roomRequest(askRepository.approve(intent.askLeave))
            }
            /*is AskIntent.askListByPage->{
                roomRequest(askRepository.askListByPage(intent.uid,intent.page,intent.size))
            }*/
        }
    }

    fun getData(uid:Int)=Pager(PagingConfig(pageSize = 1)){
        MyDataSource(askRepository,uid)
    }.flow.cachedIn(viewModelScope)
}